package IndexContent;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;
import org.apache.struts2.ServletActionContext;


public class Content {
    
    private ArrayList<HashMap<String,Object>> result; 
    private int p;      //代表瀏覽頁數
    private ArrayList<HashMap<String,Object>> productpage;
    private String pageList;
    private int category; //產品種類
    private ArrayList<HashMap<String,Object>> Topres;//最新12筆商品
    private int pid;
    private ProductInformation pt;
    private String search;
    private int TotalProduct;
    public String execute() throws Exception {        
        
        NWDB.NWDB mydb=new NWDB.NWDB();
        NWDB.IProduct ip=new NWDB.ImplProduct(mydb.getConnection());
        mydb.setIProduct(ip);

        result=mydb.GetProductByHot();
        /*Map session=(Map)ActionContext.getContext().get(ActionContext.SESSION);*/
        
        setTopres(mydb.TopProducts());//最新商品
        
        
                
        mydb.close();
        return "success";
    }
    
    public String QueryByList() throws Exception {
       
        HttpServletRequest request=ServletActionContext.getRequest();    
        setP(Integer.parseInt(request.getParameter("pid")));
        setCategory(Integer.parseInt(request.getParameter("cid")));
           
           
        NWDB.NWDB mydb=new NWDB.NWDB();
        NWDB.IProduct ip=new NWDB.ImplProduct(mydb.getConnection());
        mydb.setIProduct(ip);
        
        
        //setProductpage(mydb.GetProductByList(getP(), getCategory()));
        result=mydb.GetProductByList(getP(), getCategory());
        setTopres(mydb.TopProducts());//最新商品
        
        //取得產品頁數
        int totalPage=mydb.GetProductCount(getCategory());//result.size();
        totalPage=(int)(Math.ceil(totalPage/9.0));
        if(p==0) p=1;
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= totalPage; i++)
        {
            if (i == p)
                sb.append("<b style='font-size:1.2em; color:#ffffff;'>"+i+"</b>").append("&nbsp;&nbsp;");
            else
                sb.append(String.format("<a href='/playdoll/home/QueryByList?pid=%d&cid=%d'><b style='font-size:1.2em; color:#ffffff;'>%d<b></a>&nbsp;&nbsp;", i, getCategory(),i));
        }
        this.pageList=sb.toString();
       
        mydb.close();
        return "success";
    }
    
        
    /*public String GetProductInfoByID() throws Exception {
        
        HttpServletRequest request=ServletActionContext.getRequest();
        setP(Integer.parseInt(request.getParameter("pid")));
        
        NWDB.NWDB mydb=new NWDB.NWDB();
        NWDB.IProduct pp=new NWDB.ImplProduct(mydb.getConnection());
        mydb.setIProduct(pp);

        setRes(mydb.GetProductItemById(getP()));
        mydb.close();
        
        return "success";
    } 
    */
    
    public String SearchBar() throws Exception {
       
        HttpServletRequest request=ServletActionContext.getRequest();    
        setSearch((request.getParameter("search")));
                   
        NWDB.NWDB mydb=new NWDB.NWDB();
        NWDB.IProduct ip=new NWDB.ImplProduct(mydb.getConnection());
        mydb.setIProduct(ip);
        
        result=mydb.SearchBar(getSearch());
        setTopres(mydb.TopProducts());//最新商品
        
        mydb.close();
        return "success";
    }

    /**
     * @return the result
     */
    public ArrayList<HashMap<String,Object>> getResult() {
        return result;
    }

    /**
     * @param result the result to set
     */
    public void setResult(ArrayList<HashMap<String,Object>> result) {
        this.result = result;
    }

    /**
     * @return the p
     */
    public int getP() {
        return p;
    }

    /**
     * @param p the p to set
     */
    public void setP(int p) {
        this.p = p;
    }

    /**
     * @return the productpage
     */
    public ArrayList<HashMap<String,Object>> getProductpage() {
        return productpage;
    }

    /**
     * @param productpage the productpage to set
     */
    public void setProductpage(ArrayList<HashMap<String,Object>> productpage) {
        this.productpage = productpage;
    }

    /**
     * @return the pageList
     */
    public String getPageList() {
        return pageList;
    }

    /**
     * @param pageList the pageList to set
     */
    public void setPageList(String pageList) {
        this.pageList = pageList;
    }

    /**
     * @return the category
     */
    public int getCategory() {
        return category;
    }

    /**
     * @param category the category to set
     */
    public void setCategory(int category) {
        this.category = category;
    }

    /**
     * @return the pid
     */
    public int getPid() {
        return pid;
    }

    /**
     * @param pid the pid to set
     */
    public void setPid(int pid) {
        this.pid = pid;
    }

    /**
     * @return the pt
     */
    public ProductInformation getPt() {
        return pt;
    }

    /**
     * @param pt the pt to set
     */
    public void setPt(ProductInformation pt) {
        this.pt = pt;
    }

    /**
     * @return the search
     */
    public String getSearch() {
        return search;
    }

    /**
     * @param search the search to set
     */
    public void setSearch(String search) {
        this.search = search;
    }

    /**
     * @return the Topres
     */
    public ArrayList<HashMap<String,Object>> getTopres() {
        return Topres;
    }

    /**
     * @param Topres the Topres to set
     */
    public void setTopres(ArrayList<HashMap<String,Object>> Topres) {
        this.Topres = Topres;
    }

   
}
    
    